﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using BlueLotus7.Common;

namespace BlueLotus7.Contract
{
    public class BaseExtraData : IValidate
    {
        protected Guid relativeKey;
        protected XmlDocument extraData;
        protected DateTime insertedStamp;
        protected DateTime lastUpdatedStamp;

        public XmlNode ExtraData
        {
            get { return extraData; }
        }
        public DateTime InsertedStamp
        {
            get { return insertedStamp; }
        }
        public DateTime LastUpdatedStamp
        {
            get { return lastUpdatedStamp; }
        }

        public BaseExtraData(Guid relativeKey)
        {
            insertedStamp = DateTime.Now;
            lastUpdatedStamp = insertedStamp;
            extraData = new XmlDocument();
            InitializeXmlDocument();
        }

        public BaseExtraData(Guid relativeKey, XmlDocument extraData, DateTime insertedStamp, DateTime lastUpdatedStamp)
        {
            this.relativeKey = relativeKey;
            this.extraData = extraData;
            this.insertedStamp = insertedStamp;
            this.lastUpdatedStamp = lastUpdatedStamp;
        }

        protected void InitializeXmlDocument()
        {
            extraData.AppendChild(extraData.CreateNode(XmlNodeType.XmlDeclaration, "", ""));

            XmlElement rootNode = extraData.CreateElement("", "BlueLotus7", "");
            extraData.AppendChild(rootNode);
        }

        #region IValidate Members

        public bool IsValid
        {
            get
            {
                try
                {
                    Validate();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public void Validate()
        {
            XmlNode rootNode = extraData.SelectSingleNode("BlueLotus7");
            if (rootNode == null)
            {
                throw new InvalidObjectException("extraData");
            }
        }

        #endregion
    }
}
